package com.algorithm;

/**
 * @author: sun.hongliang
 * @create: 2019/07/20 11:27
 */
public class LongestCommonPrefixTest {
    public static void main(String[] args) {

    }

    public static String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        int minLen = strs[0].length();
        for (String str : strs) {
            minLen = Math.min(minLen, str.length());
        }
        StringBuilder result = new StringBuilder();
        k:
        for (int i = 0; i < minLen; i++) {
            char temp = strs[0].charAt(i);
            for (int j = 0; j < strs.length; j++) {
                if (strs[j].charAt(i) != temp) {
                    break k;
                }
            }
            result.append(temp);
        }
        return result.toString();
    }
}
